package com.example.demo.controller.admin;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.MajorMap;
import com.example.demo.po.Major;
import com.example.demo.service.MajorService;
import com.example.demo.service.impl.MajorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/major")
public class MajorController {

    @Autowired
    MajorMap majorMap;

    @Autowired
    MajorService majorService;

    //crud
    @PostMapping("/save")
    public Result SaveMajor(@RequestBody Major major){
          Result res =   majorService.SaveMajor(major);
            return res;
    }
    @GetMapping("/delete/{id}")
    public Result DeleteMajor(@PathVariable int id){

        majorMap.DeleteMajor(id);
        return ResultUtil.success("删除成功");
    }
    @PostMapping("/update")
    public Result UpdateMajor(@RequestBody Major major){

        Result res =  majorService.UpdateMajor(major);
        return res;
    }

    @GetMapping("/get")
    public Result GetMajorList(){

        return ResultUtil.success(majorMap.GetMajorList());

    }

    @GetMapping("/search")
    public Result GetMajorListByName(@RequestParam("name") String name){

        if(name == null || name.isEmpty()){
            return ResultUtil.success(majorMap.GetMajorList());
        }

        return ResultUtil.success(majorMap.GetMajorByName("%"+name+"%"));

    }

}
